<?php

namespace app\model\v1\recovery;

use app\model\system\admin\SystemAdmin;
use crmeb\traits\ModelTrait;
use think\facade\Db;
use think\Model;


class ConfigCost extends Model
{


    use ModelTrait;

    protected $updateTime = false;
    protected $createTime = false;

    protected $hidden = [
    //    'create_time','update_time','delete_time'
    ];

    protected $table = 'eb_config_cost';




    /**
     * 添加打包费
     * @param $param
     * @return int|string
     */
    public static function addConfigCost($param = [])
    {
        $add = [
            "pack_cost"     => $param["pack_cost"],
            "type"          => $param["type"] ?? '',
            "create_time"   => time(),
        ];
        return self::insert($add);
    }



    /**
     * 获取打包费管理列表
     * @param $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     * @throws \think\exception\DbException
     */
    public static function getConfigCostList($param = [])
    {
        $order = ["id"=>"desc"];
        //按照创建时间排序
        if(!empty($param["order_ctime"])){
            if(!empty($param["order_ctime_sc"])){
                $order = ["id"=>"desc", "create_time"=> $param["order_ctime_sc"]];
            }else{
                //默认按照字段降序排序
                $order = ["id"=>"desc", "create_time"=>"desc"];
            }
        }

        $data["data"] = self::field ("*,FROM_UNIXTIME(create_time) as create_time,
            case when status=1 then '启用' 
                when status=2 then '禁用' 
                end as status_text,
            case when type=1 then 'app' 
                when type=2 then 'h5' 
                when type=3 then '小程序' 
                end as type_text
            ")
            ->where(function ($query) use ($param){
                $query->whereNull("delete_time");
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                //根据关键字搜索
                if(!empty($param["word"])) $query->where("pack_cost",'like', '%'.$param["word"].'%');
                //根据类型搜索
                if(!empty($param["type"])) $query->where("type", $param["type"]);
            })
            ->order($order)
            ->page($param["page"], $param['limit'])
            ->select()->toArray();

        $data["count"] = self::field ("*")
            ->where(function ($query) use ($param){
                $query->whereNull("delete_time");
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                if(!empty($param["word"])) $query->where("pack_cost",'like', '%'.$param["word"].'%');
                if(!empty($param["type"])) $query->where("type", $param["type"]);
            })
            ->count();

        return $data;
    }


    /**
     * 打包费管理列表 - 编辑
     * @param $param
     * @return ConfigBanner
     */
    public static function updateConfigCost($param = [])
    {
        $update["update_time"] = time();  //更新时间
        if(!empty($param["pack_cost"])) $update["pack_cost"] = $param["pack_cost"];
        if(!empty($param["type"]))          $update["type"] = $param["type"];

        return self::where("id", $param['id'])->update($update);
    }



    /**
     * 打包费管理列表 - 启用禁用
     * @param $param
     * @return string
     */
    public static function enableConfigCost($param = [])
    {
        $status = self::where("id", $param['id'])->value("status");
        $msg = '';
        if($status == 1){   //1启用 2禁用
            $enable = 2;
            $msg = "禁用成功";
        }else{
            $enable = 1;
            $msg = "启用成功";
        }
        self::where("id", $param['id'])->update(['status' => $enable]);
        return $msg;
    }




    public static function  getCostPriceText($set_type,$types,$product_type)
    {

	 return self::whereNull("delete_time")->where(["status" => 1, "set_type" => $set_type, "type" => $types, "product_type" => $product_type])->value("pack_cost") ?? 0;

    }



}